<?php namespace Home\Model;
use Hdphp\Model\Model;
class User extends Model{
	protected $table="user";  //载入用户表
		//自动验证
	protected $validate = array(
		array('username','required','用户名不能为空',3,3),
		array('username','email','邮箱格式错误',3,3),
		array('password','required','密码不能为空',3,3),
		// array('oldPwd','required','密码不能为空',3,3),
		// array('confirmPwd','required','密码不能为空',3,3)
	);
		//添加注册用户
	public function store($username,$password){

		if(!$this->create()) return false;
		$password = $this->encrypt($username, $password);
		$data=array(
			'username'=>$username,
			'password'=>$password
		);	
		$this->add($data);
		return true;
	}
		//加密
	public function encrypt($username,$password){
		return md5(md5($username).md5($password).'xiaoming');
	}
		//登录
	public function Vlogin($username,$password){
		if(!$this->create()) return false;
		$oldData = $this->where("username='{$username}'")->find();
		if(!$oldData){
			$this->error("用户名{$username}不存在");
			return false;
		}
		$password = $this->encrypt($username,$password);
		if($password != $oldData['password']){
			$this->error="密码错误";
			return false;
		}
		if(Q('post.auto')){
			//设置7天保存
			setcookie(session_name(),session_id(),time()+3600*24*7,'/');
		}
		//把用户名压入SESSION，后面要使用
		$_SESSION['username'] = $username;
		$_SESSION['uid'] = $oldData['uid'];
		$_SESSION['neckname'] = $oldData['neckname'];
		return true;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}




?>